'''
Created on Oct 9, 2010

@author: oabalbin

Read a file with the list of outliers
and identifies the outliers that are common
to all lists.
105     53400   uc010cre.1_KCNJ12       chr17:21259247-21260549 29.0281735048   1       46.0569 0.131358        0.0011
'''
import sys
import operator
import numpy as np

from optparse import OptionParser
import RNAseq.arraytools.subset_genes as sg
from collections import defaultdict


def read_list_file(inputfile_name, thpval):    
    inputfile=open(inputfile_name)
    outliers=defaultdict()
    cohort_sizes = inputfile.next().strip('\n').split('\t')
    inputfile.next()
    for line in inputfile:
        fields = line.strip('\n').split('\t')
        if float(fields[8]) <= thpval and fields[3].startswith('chr'):
            outliers[fields[2]] = fields[3:]
            
    inputfile.close()
    return outliers, cohort_sizes


if __name__ == '__main__':
    
    optionparser = OptionParser("usage: %prog [options] ")
    optionparser.add_option("-d", "--outfolder", dest="outfolder",
                            help="output folder")
    optionparser.add_option("-r", "--commonName", dest="commonName",
                            help="output folder")
    
    (options, args) = optionparser.parse_args()

    thosefiles = sg.read_files_folder(options.outfolder,options.commonName)
    
    thpval=0.05
    all_outliers, cohort_sizes=defaultdict(list), defaultdict()

    for i, thfile in enumerate(thosefiles):
        tissue_name=thfile.split('/')[-1]
        
        thoutliers, thcohort_sizes = read_list_file(thfile, thpval)
        all_outliers[tissue_name] = thoutliers
        # In field 1 is the total size of the cohort, in 2 the number of tumors and in 3 the number of benign
        cohort_sizes[tissue_name] = int(thcohort_sizes[1])
        
    cohort_sizes = sorted(cohort_sizes.iteritems(), key=operator.itemgetter(1), reverse=True)    
    
    tissue_cols = defaultdict()
    for j,tissue in enumerate(cohort_sizes):
        tissue_name = tissue[0].split(options.commonName)
        tissue_name = tissue_name[1].strip('_').split('_')[0]
        tissue_cols[tissue_name] = (j,tissue[1])
        print tissue_name, tissue_cols[tissue_name]

    header = [ "cohort_name", "gene.name", "gene.location", "gene.mean_copa_score", "Nsamples", "max_exp","median_exp", "gene.pvalue"]
    outfile = open(options.outfolder+options.commonName+'_'+'common_outliers.tmp','w')
    outfile.write(",".join(header).replace(',', '\t')+'\n')
        
    k=0
    common_outliers_dict=defaultdict()
    cohort_sizes_tmp= cohort_sizes[:]
 
    while (len(cohort_sizes) > 1):
        common_outliers = set()
        #header_lp = sum(map(list,cohort_sizes),[]) 
        #outfile.write("cohorts:\t"+",".join(map(str,header_lp)).replace(',','\t')+'\n')
        
        for i, thtissue in enumerate(cohort_sizes):
            thtissue=thtissue[0]
            thoutliers = all_outliers[thtissue]
            
            if i == 0:
                common_outliers_tmp = set(thoutliers.keys())
            elif i==1:
                common_outliers = common_outliers_tmp.intersection(set(thoutliers.keys()))
                for outl in common_outliers:
                    if outl not in common_outliers_dict.keys():
                        common_outliers_dict[outl]=k
                        #print outl, k
                        k+=1
            else:
                new_common_outliers = common_outliers.intersection(set(thoutliers.keys()))
                for outl in new_common_outliers:
                    if outl not in common_outliers_dict.keys():
                        common_outliers.add(outl)
                        common_outliers_dict[outl]=k
                        #print outl, k
                        k+=1
            
            
            '''
            if i == 0:
                common_outliers = set(thoutliers.keys())
            else:
                common_outliers.intersection_update(set(thoutliers.keys()))
            '''
        cohort_sizes.pop()
    counter = k
    # Get Outliers present only in one tissue
    print counter
    cohort_sizes = cohort_sizes_tmp[:]
    single_outliers, single_outliers_dict=[], defaultdict()
    for i, thtissue in enumerate(cohort_sizes):
        thtissue=thtissue[0]
        thoutliers = all_outliers[thtissue]        
        if i == 0:
            single_tissue_outl =  set(thoutliers).difference(common_outliers)
            single_outliers = common_outliers
        else:
            single_tissue_outl =  set(thoutliers).difference(single_outliers)
        
        for outl in single_tissue_outl:
            single_outliers.add(outl)
            single_outliers_dict[outl]=counter
            counter+=1
            
    print  counter, len(single_outliers_dict)
    cohort_sizes = cohort_sizes_tmp[:]
    # outlier matrix
    print len(common_outliers_dict)+len(single_outliers_dict)
    outlier_matrix = np.zeros( ((len(common_outliers_dict)+len(single_outliers_dict)), len(all_outliers)) )
    while (len(cohort_sizes) > 1):
        common_outliers = set()
        #header_lp = sum(map(list,cohort_sizes),[]) 
        #outfile.write("cohorts:\t"+",".join(map(str,header_lp)).replace(',','\t')+'\n')
        
        for i, thtissue in enumerate(cohort_sizes):
            thtissue=thtissue[0]
            thoutliers = all_outliers[thtissue]
            
            if i == 0:
                common_outliers = set(thoutliers.keys())
            else:
                common_outliers.intersection_update(set(thoutliers.keys()))

                
        for thtissue in cohort_sizes:
            thtissue=thtissue[0]
            thoutliers = all_outliers[thtissue]
            tissue_name = thtissue.split(options.commonName)
            tissue_name = tissue_name[1].strip('_').split('_')[0]
            mcol,csize =  tissue_cols[tissue_name][0], tissue_cols[tissue_name][1]
            
            for tho in common_outliers:
                j = common_outliers_dict[tho]
                outlier_matrix[j,mcol] = int(thoutliers[tho][2])#/float(csize)
                
        cohort_sizes.pop()
    
    cohort_sizes = cohort_sizes_tmp[:]    
    # include the single outliers in the overall matrix 
    for thtissue in cohort_sizes:
        thtissue=thtissue[0]
        thoutliers = all_outliers[thtissue]
        tissue_name = thtissue.split(options.commonName)
        tissue_name = tissue_name[1].strip('_').split('_')[0]
        mcol,csize =  tissue_cols[tissue_name][0], tissue_cols[tissue_name][1]
        
        for tho, j in single_outliers_dict.iteritems():
            try:
                outlier_matrix[j,mcol] = int(thoutliers[tho][2])#/float(csize)
            except KeyError:
                continue

#outfile.write("ntissues|tgene.name\t"+",".join(tissue_cols.keys).replace(',','\t')+'\n')
#outfile.write("ntissues|tgene.name\t"+",".join().replace(',','\t')+'\n')

# update common dictionary in order to include outliers present in single tissues

common_outliers_dict.update(single_outliers_dict)
for tho, i in common_outliers_dict.iteritems():
    ntissues = len(outlier_matrix[i, outlier_matrix[i,:] > 0.0])
    line = map(str,list(outlier_matrix[i,:])) 
    outfile.write(str(ntissues)+'\t'+tho+'\t'+",".join(line).replace(',','\t')+'\n')
    
outfile.close()


'''
    while (len(cohort_sizes) > 1):
        header_lp = sum(map(list,cohort_sizes),[]) 
        outfile.write("cohorts:\t"+",".join(map(str,header_lp)).replace(',','\t')+'\n')
            
        for thtissue in cohort_sizes:
            thtissue=thtissue[0]
            thoutliers = all_outliers[thtissue]
            tissue_name = thtissue.split(options.commonName)
            tissue_name = tissue_name[1].strip('_').split('_')[0]
            mcol,csize =  tissue_cols[tissue_name][0], tissue_cols[tissue_name][1]
            
            for j, tho  in common_outliers_dict.iteritems():
#            for tho in common_outliers:
                
                outlier_matrix_temp = np.zeros(len(all_outliers))
                outlier_matrix_temp[mcol] = int(thoutliers[tho][2])#/float(csize)
                outlier_matrix = np.vstack((outlier_matrix,outlier_matrix_temp))
                #outfile.write(thtissue+'\t'+tho+'\t'+",".join(thoutliers[tho]).replace(',','\t')+'\n')
                print thtissue, tho, outlier_matrix_temp 
            
            #print outlier_matrix

        cohort_sizes.pop()
            
    outfile.close()

'''